package com.wit.service.impl;

import java.util.List;

import hibernate.HibernateSessionFactory;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.wit.dao.impl.EmployeePositionservice;
import com.wit.po.Employeeposition;
import com.wit.po.JSONPosition;

public class EmployeePositionserviceImp implements EmployeePositionservice {
	public void AddPosition( String employeePosId, String positionName,String positionDes,String positionOwn,
	 Double firstrate,Double conrate,Double monthrate, Double seasonrate,Double yearrate,Double addEmprate,
	 Double yrateC,Double yrateE,Double grateZ,Double grateE,Double zrate1,Double zrate2,Double zrate3,Double zrate4,
	 Double zrate5,Double areaMrate1,Double areaMrate2,Double areaMrate3,Double areaMrate4,Double firstY,Double secondY,
	 Double areaY,Double areaSrate1,Double areaSrate2)
	{
		Session session=HibernateSessionFactory.getSession();
		Transaction tx=session.beginTransaction();
		Employeeposition ep=new Employeeposition();
		ep.setEmployeePosId(employeePosId);
		ep.setPositionName(positionName);
		ep.setPositionDes(positionDes);
		ep.setPositionOwn(positionOwn);
	    ep.setYrateC(yrateC);
	    ep.setYrateE(yrateE);
	    ep.setGrateZ(grateZ);
		ep.setGrateE(grateE);
		ep.setFirstrate(firstrate);
		ep.setConrate(conrate);
		ep.setMonthrate(monthrate);
		ep.setSeasonrate(seasonrate);
		ep.setYearrate(yearrate);
		ep.setAddEmprate(addEmprate);
		ep.setZrate1(zrate1);
		ep.setZrate2(zrate2);
		ep.setZrate3(zrate3);
		ep.setZrate4(zrate4);
		ep.setZrate5(zrate5);
		ep.setAreaMrate1(areaMrate1);
		ep.setAreaMrate2(areaMrate2);
		ep.setAreaMrate3(areaMrate3);
		ep.setAreaMrate4(areaMrate4);
		ep.setFirstY(firstY);
		ep.setSecondY(secondY);
		ep.setAreaY(areaY);
		ep.setAreaSrate1(areaSrate1);
		ep.setAreaSrate2(areaSrate2);
		session.save(ep);
		tx.commit();
		session.close();
	}
	public JSONPosition ReturnPostion(String positionName)
	{
	    Session session=HibernateSessionFactory.getSession();
	    try{
	    Employeeposition emp=(Employeeposition)session.createQuery("from Employeeposition where positionName='"+positionName+"'").uniqueResult();
	    JSONPosition jso=new JSONPosition();
	    jso.setAddEmprate(emp.getAddEmprate());
	    jso.setAreaMrate1(emp.getAreaMrate1());
	    jso.setAreaMrate2(emp.getAreaMrate2());
	    jso.setAreaMrate3(emp.getAreaMrate3());
	    jso.setAreaMrate4(emp.getAreaMrate4());
	    jso.setAreaSrate1(emp.getAreaSrate1());
	    jso.setAreaSrate2(emp.getAreaSrate2());
	    jso.setAreaY(emp.getAreaY());
	    jso.setConrate(emp.getConrate());
	    jso.setEmployeePosId(emp.getEmployeePosId());
	    jso.setFirstrate(emp.getFirstrate());
	    jso.setSecondY(emp.getSecondY());
	    jso.setFirstY(emp.getFirstY());
	    jso.setGrateE(emp.getGrateE());
	    jso.setGrateZ(emp.getGrateZ());
	    jso.setMonthrate(emp.getMonthrate());
	    jso.setZrate5(emp.getZrate5());
	    jso.setZrate4(emp.getZrate4());
	    jso.setZrate3(emp.getZrate3());
	    jso.setZrate2(emp.getZrate2());
	    jso.setZrate1(emp.getZrate1());
	    jso.setYrateE(emp.getYrateE());
	    jso.setYrateC(emp.getYrateC());
	    jso.setYearrate(emp.getYearrate());
	    jso.setSeasonrate(emp.getSeasonrate());
	    jso.setPositionDes(emp.getPositionDes());
	    jso.setPositionName(emp.getPositionName());
	    jso.setPositionOwn(emp.getPositionOwn());
	    session.close();
	    return jso;
	    }catch(Exception e)
	    {
	    	return null;
	    }
	}
	public void mendPosition(String employeePosId, String positionName,String positionDes,String positionOwn,
			 Double firstrate,Double conrate,Double monthrate, Double seasonrate,Double yearrate,Double addEmprate,
			 Double yrateC,Double yrateE,Double grateZ,Double grateE,Double zrate1,Double zrate2,Double zrate3,Double zrate4,
			 Double zrate5,Double areaMrate1,Double areaMrate2,Double areaMrate3,Double areaMrate4,Double firstY,Double secondY,
			 Double areaY,Double areaSrate1,Double areaSrate2)
	{
		Session session=HibernateSessionFactory.getSession();
		Transaction tx=session.beginTransaction();
		Query query=session.createQuery("update Employeeposition set positionName='"+positionName+"', positionDes='"+positionDes+"', positionOwn='"+positionOwn+"',firstrate='"+firstrate+"', conrate='"+conrate+"', monthrate='"+monthrate+"',  seasonrate='"+seasonrate+"', yearrate='"+yearrate+"', addEmprate='"+addEmprate+"',yrateC='"+yrateC+"', yrateE='"+yrateE+"', grateZ='"+grateZ+"', grateE='"+grateE+"', zrate1='"+zrate1+"', zrate2='"+zrate2+"', zrate3='"+zrate3+"', zrate4='"+zrate4+"',zrate5='"+zrate5+"', areaMrate1='"+areaMrate1+"', areaMrate2='"+areaMrate2+"', areaMrate3='"+areaMrate3+"', areaMrate4='"+areaMrate4+"', firstY='"+firstY+"', secondY='"+secondY+"',areaY='"+areaY+"', areaSrate1='"+areaSrate1+"', areaSrate2='"+areaSrate2+"' where employeePosId='"+employeePosId+"'");
	    query.executeUpdate();
		tx.commit();
		session.close();
	}
	public void DelPosition(String employeePosId)
	{
		Session session=HibernateSessionFactory.getSession();
		Transaction tx=session.beginTransaction();
		Query query=session.createQuery("delete Employeeposition where employeePosId='"+employeePosId+"'");
		query.executeUpdate();
		tx.commit();
		session.close();
	}
	public  List<Employeeposition> ExistPosition()
	{
		Session session=HibernateSessionFactory.getSession();
		try{
        List<Employeeposition> list=session.createQuery("from Employeeposition").list();
        return list;
		}catch(Exception e){
			return null;
		}
	}
	public static void main(String[] argv)
	{
		EmployeePositionserviceImp em=new EmployeePositionserviceImp();
	
	}
}
